package cn.sunxyz.data.infer.parser.reduce;

import cn.sunxyz.data.infer.ex.SQLParserException;
import cn.sunxyz.data.infer.parser.AbstractNameParser;
import cn.sunxyz.data.infer.parser.NameParser;
import cn.sunxyz.data.infer.rule.NameRule;
import cn.sunxyz.data.support.EntitySupport;

import java.util.stream.Collectors;

/**
 * Created by yangrd on 2017/6/2.
 */
public class ReduceParser extends AbstractNameParser {

    @Override
    public String parser(Class<?> entityCls, Class<?> targetCls, String input) throws Exception {
        if (NameRule.contains(input)) {
            String table = EntitySupport.getTableName(entityCls);
            if (NameRule.isQuery(input)) {
                if (entityCls.equals(targetCls)) {
                    return "SELECT * FROM " + table;
                } else {
                    String columns = EntitySupport.getColumnFields(targetCls).stream().map(EntitySupport::getColumn).collect(Collectors.joining(" , "));
                    return "SELECT " + columns + " FROM " + table;
                }
            } else {//删除
                return "DELETE  FROM " + table;
            }
        } else {
            throw new SQLParserException("method name Does not meet the rules");
        }
    }
}
